SWEP.Primary.Sound			= Sound( "Weapon_Pistol.Single" )
SWEP.Primary.Damage			= 30
SWEP.Primary.NumShots		= 1
SWEP.Primary.Delay			= 0.15

SWEP.ConeMax				= 0.02
SWEP.ConeMin				= 0.01
SWEP.ConeRamp				= 2

SWEP.Primary.ClipSize		= 8
SWEP.Primary.DefaultClip	= 0
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "pistol"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

SWEP.WalkSpeed				= 250
SWEP.HoldType				= "pistol"
SWEP.IronSightsHoldType		= "ar2"

SWEP.IronSightsPos			= Vector( 0, 0, 0 )


function SWEP:Initialize()
	if !( IsValid( self ) ) then return end

	self:SetClip1( self.Primary.ClipSize )
	self:SetWeaponHoldType( self.HoldType )
	self:SetDeploySpeed( 1.1 )

	-- Maybe we didn't want to convert the weapon to the new system...
	if self.Cone then
		self.ConeMin = self.ConeIronCrouching
		self.ConeMax = self.ConeMoving
		self.ConeRamp = 2
	end
end

function SWEP:GetCone()
	if !( self.Owner:OnGround() ) || ( self.ConeMax == self.ConeMin ) then return self.ConeMax end

	local basecone = self.ConeMin
	local conedelta = self.ConeMax - basecone

	local multiplier = math.min( self.Owner:GetVelocity():Length() / self.WalkSpeed, 1 ) * 0.5
	if !( self.Owner:Crouching() ) then multiplier = multiplier + 0.25 end
	if !( self:GetIronsights() ) then multiplier = multiplier + 0.25 end

	return basecone + conedelta * multiplier ^ self.ConeRamp
end

function SWEP:PrimaryAttack()
	if !( self:CanPrimaryAttack() ) then return end
	self:SetNextPrimaryFire(CurTime() + self.Primary.Delay)

	self:EmitFireSound()
	self:TakeAmmo()
	self:ShootBullets( self.Primary.Damage, self.Primary.NumShots, self:GetCone() )
	self.IdleAnimation = CurTime() + self:SequenceDuration()
end

function SWEP:EmitFireSound()
	self:EmitSound( self.Primary.Sound )
end

function SWEP:SetIronsights( b )
	self:SetDTBool( 0, b )

	if self.IronSightsHoldType then
		self:SetWeaponHoldType( b && self.IronSightsHoldType || self:GetHoldType() )
	end
end

function SWEP:Deploy()
	self:SetNextReload( 0 )
	self:SetIronsights( false )

	self.IdleAnimation = CurTime() + self:SequenceDuration()

	return true
end

function SWEP:Holster()
	return true
end

function SWEP:TakeAmmo()
	self:TakePrimaryAmmo( 1 )
end

function SWEP:Reload()
	if self:GetIronsights() then
		self:SetIronsights( false )
	end

	if ( self:GetNextReload() <= CurTime() ) && ( self:DefaultReload( ACT_VM_RELOAD ) ) then
		self.IdleAnimation = CurTime() + self:SequenceDuration()
		self:SetNextReload( self.IdleAnimation )
		self.Owner:DoReloadEvent()

		if self.ReloadSound then
			self:EmitSound( self.ReloadSound )
		end
	end
end

function SWEP:GetIronsights()
	return self:GetDTBool( 0 )
end

function SWEP:CanPrimaryAttack()
	--if ( GAMEMODE:GetState() ~= STATE_PLAYING ) then return false end

	if ( self:Clip1() <= 0 ) then
		self:EmitSound( "Weapon_Pistol.Empty" )
		self:SetNextPrimaryFire( CurTime() + math.max( 0.1, self.Primary.Delay ) )
		return false
	end

	return self:GetNextPrimaryFire() <= CurTime()
end

function SWEP:SecondaryAttack()
	if ( self:GetNextSecondaryFire() <= CurTime() ) then
		self:SetIronsights( true )
	end
end

function SWEP:OnRestore()
	self:SetIronsights( false )
end

function GenericBulletCallback( attacker, tr, dmginfo )
	local ent = tr.Entity

	if IsValid( ent ) && !( ent:IsPlayer() ) then
		local phys = ent:GetPhysicsObject()
		if ( ent:GetMoveType() == MOVETYPE_VPHYSICS ) && IsValid( phys ) && phys:IsMoveable() then
			ent:SetPhysicsAttacker( attacker )
		end
	end
end

function SWEP:SendWeaponAnimation()
	self:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
end

SWEP.BulletCallback = GenericBulletCallback
function SWEP:ShootBullets( dmg, numbul, cone )
	local owner = self.Owner
	self:SendWeaponAnimation()
	owner:DoAttackEvent()

	owner:FireBullets({ Num = numbul, Src = owner:GetShootPos(), Dir = owner:GetAimVector(), Spread = Vector(cone, cone, 0), Tracer = 1, TracerName = self.TracerName, Force = dmg * 0.1, Damage = dmg, Callback = self.BulletCallback })
end

local ActIndex = {
	[ "pistol" ] 		= ACT_HL2MP_IDLE_PISTOL,
	[ "smg" ] 			= ACT_HL2MP_IDLE_SMG1,
	[ "grenade" ] 		= ACT_HL2MP_IDLE_GRENADE,
	[ "ar2" ] 			= ACT_HL2MP_IDLE_AR2,
	[ "shotgun" ] 		= ACT_HL2MP_IDLE_SHOTGUN,
	[ "rpg" ]	 		= ACT_HL2MP_IDLE_RPG,
	[ "physgun" ] 		= ACT_HL2MP_IDLE_PHYSGUN,
	[ "crossbow" ] 		= ACT_HL2MP_IDLE_CROSSBOW,
	[ "melee" ] 		= ACT_HL2MP_IDLE_MELEE,
	[ "slam" ] 			= ACT_HL2MP_IDLE_SLAM,
	[ "normal" ]		= ACT_HL2MP_IDLE,
	[ "fist" ]			= ACT_HL2MP_IDLE_FIST,
	[ "melee2" ]		= ACT_HL2MP_IDLE_MELEE2,
	[ "passive" ]		= ACT_HL2MP_IDLE_PASSIVE,
	[ "knife" ]			= ACT_HL2MP_IDLE_KNIFE,
	[ "duel" ]      	= ACT_HL2MP_IDLE_DUEL,
	[ "revolver" ]		= ACT_HL2MP_IDLE_REVOLVER
}

function SWEP:SetWeaponHoldType( t )

	t = string.lower( t )
	local index = ActIndex[ t ]
	
	if ( index == nil ) then
		Msg( "SWEP:SetWeaponHoldType - ActIndex[ \""..t.."\" ] isn't set! (defaulting to normal)\n" )
		t = "normal"
		index = ActIndex[ t ]
	end

	self.ActivityTranslate = {}
	self.ActivityTranslate [ ACT_MP_STAND_IDLE ] 				= index
	self.ActivityTranslate [ ACT_MP_WALK ] 						= index+1
	self.ActivityTranslate [ ACT_MP_RUN ] 						= index+2
	self.ActivityTranslate [ ACT_MP_CROUCH_IDLE ] 				= index+3
	self.ActivityTranslate [ ACT_MP_CROUCHWALK ] 				= index+4
	self.ActivityTranslate [ ACT_MP_ATTACK_STAND_PRIMARYFIRE ] 	= index+5
	self.ActivityTranslate [ ACT_MP_ATTACK_CROUCH_PRIMARYFIRE ] = index+5
	self.ActivityTranslate [ ACT_MP_RELOAD_STAND ]		 		= index+6
	self.ActivityTranslate [ ACT_MP_RELOAD_CROUCH ]		 		= index+6
	self.ActivityTranslate [ ACT_MP_JUMP ] 						= index+7
	self.ActivityTranslate [ ACT_RANGE_ATTACK1 ] 				= index+8
	self.ActivityTranslate [ ACT_MP_SWIM_IDLE ] 				= index+8
	self.ActivityTranslate [ ACT_MP_SWIM ] 						= index+9
	
	-- "normal" jump animation doesn't exist
	if ( t == "normal" ) then
		self.ActivityTranslate [ ACT_MP_JUMP ] = ACT_HL2MP_JUMP_SLAM
	end
	
	-- these two aren't defined in ACTs for whatever reason
	if ( t == "knife" ) || ( t == "melee2" ) then
		self.ActivityTranslate [ ACT_MP_CROUCH_IDLE ] = nil
	end
end

SWEP:SetWeaponHoldType( "pistol" )

function SWEP:TranslateActivity( act )
	if self:GetIronsights() && self.ActivityTranslateIronSights then
		return self.ActivityTranslateIronSights[act] or -1
	end

	return self.ActivityTranslate && self.ActivityTranslate[act] or -1
end
